<!DOCTYPE html>
<html>
    <?php
    /*
     * This contains the functions to send or set a order on hold
     */
    require_once "../../DB/db.php";
    include_once '../../class/UserClass.php';
    $message = '';  // message if the send function completed
    $result = '';   // to check if queri complete
    if ($user->isLoggedOn() && $user->isMedarbeider()) {
        if (isset($_REQUEST['send'])) { //if the employee have sent the order
            $sql = 'UPDATE orders SET sendingdate=:sendingdate WHERE oid=:oid'; //queri
            $sth = $db->prepare($sql);
            $sth->bindParam(':sendingdate', date("Y-m-d H:m:s")); // sets sending date
            $sth->bindParam(':oid', $_REQUEST['send']); // what order to send
            $result = $sth->execute();
            if ($result) { //if complete
                $message = '<p>Bestillingen er sendt!</p><br><a href="../myEmpSite.php?orders=orders">Tilbake til behandling av bestillinger</a>';
            }
        } else if (isset($_REQUEST['pending'])) { // if the items is sold out
            $pending = date("0-0-0 00:00:00");  // sets the default value
            $sql = 'UPDATE orders SET sendingdate=:sendingdate WHERE oid=:oid'; //queri
            $sth = $db->prepare($sql);
            $sth->bindParam(':sendingdate', $pending);  // sets the sendingdate to default
            $sth->bindParam(':oid', $_REQUEST['pending']);// what order to set on pending
            $result = $sth->execute();
            if ($result) { // if complete
                $message = '<p>Bestillingen er satt til pendler!</p><br><a href="../myEmpSite.php?orders=orders">Tilbake til behandling av bestillinger</a>';
            }
        }
        ?>
        <head>
            <title>Nettbutikk</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" type="text/css" href="../../CSS/mainFrame.css" />
            <link rel="stylesheet" type="text/css" href="../../CSS/content.css" />
        </head>
        <body>
            <div class="wrapper">
                <?php
                include_once "../../MainFrame/header.php";
                include_once "../../MainFrame/menuBar.php";
                include_once "../../MainFrame/sideBar.php";
                ?>

                <div class="content">
                    <div class="empEditOrderContent">
                        <?php
                        if ($message != '') { // success message
                            echo '<div class="successEdit">' . $message . '</div>';
                        }

                        if (isset($_REQUEST['orderID'])) { // if orderID is set display what items to send
                            $res = $db->query('SELECT * from orders WHERE oid=' . $_REQUEST['orderID']);
                            $res1 = $db->query('SELECT * from orderitems WHERE oid=' . $_REQUEST['orderID']);
                            if ($row = $res->fetch()) { //get orderitems and order values
                                echo '<h1>Ordre ID: ' . $row['oid'] . '</h1>'; //display the values of the order
                                echo '<h3>Bestillings dato: ' . $row['orderdate'] . '</h3>';
                                echo '<table id="empEditOrderTable">'; //creates a table to list the orderitems                      
                                echo "<tr><th>Vare ID</th><th>vare navn</th><th>Antall</th></tr>";
                                while ($row1 = $res1->fetch()) {
                                    $res2 = $db->query('SELECT * from item WHERE vid=' . $row1['vid']);
                                    if ($row2 = $res2->fetch()) { //display the values for the orderitem
                                        echo '<tr>';
                                        echo '<td>' . $row1['vid'] . '</td>';
                                        echo '<td>' . $row2['name'] . '</td>';
                                        echo '<td>' . $row1['numberof'] . '</td>';
                                        echo '</tr>';
                                    }
                                }
                                echo '<tr><td></td></tr><tr><td></td></tr>';
                                echo '</table>';
                                //display the user informastion for user that ordered
                                $res3 = $db->query('SELECT * from users WHERE uid=' . $row['uid']);
                                if ($row3 = $res3->fetch()) {   
                                    echo '<p>Navn: ' . $row3['givenname'] . ' ' . $row3['surename'] . '</p>';
                                    echo '<p>Adresse: ' . $row3['address'] . '</p>';

                                    $res4 = $db->query('SELECT * from countries WHERE cid=' . $row3['country']);
                                    if ($row4 = $res4->fetch()) {   // displays the country
                                        echo '<p>Land: ' . $row4['country'] . '</p>';
                                    }
                                    echo '<p>Poststed: ' . $row3['postnr'] . ' ' . $row3['poststed'] . '</p>';
                                    echo '<a href="empEditOrder.php?pending=' . $row['oid'] . '" ><input type="button" name="pending" value="pendle"></a>';
                                    echo '<a href="empEditOrder.php?send=' . $row['oid'] . '" ><input type="button" name="send" value="send"></a>';
                                }   //buttons the choose what to do with the order
                            }
                        }
                        ?>
                    </div>
                </div>

                <?php
            }
            include_once "../../MainFrame/footer.php";
            ?>
        </div>
    </body>
</html>